﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Easyleave.Logic.Domain;
using System.Configuration;
using System.ServiceModel;
using Easyleave.Logic;
using System.Data.Linq;

namespace Easyleave.Workflow
{
    public class LeaveDataManager
    {
        EasyleaveDataContext context;

        public LeaveDataManager() 
        {
            string connectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
            context = new EasyleaveDataContext(connectionString);
        }

        public void UpdateCurrentState(Guid instanceId, string stateName)
        {
            LeaveRecord record = (from leaveRec in context.LeaveRecords where leaveRec.workflowInstanceId == instanceId select leaveRec).First();
            record.currentState = stateName;
            context.SubmitChanges();
        }

        public void UpdateNextRecommenderId(Guid instanceId, Guid newRecommenderId)
        {
            LeaveRecord record = (from leaveRec in context.LeaveRecords where leaveRec.workflowInstanceId == instanceId select leaveRec).First();
            record.nextRecommenderId = newRecommenderId;
            context.SubmitChanges();
        }


        public IList<LeaveDto> GetRecordsByRecommender(Guid recommenderId)
        {
            var records = from leaveRec in context.LeaveRecords where leaveRec.nextRecommenderId == recommenderId select leaveRec;
            List<LeaveDto> results = new List<LeaveDto>();
            foreach (LeaveRecord record in records)
            {
                results.Add(ConvertTypes(record));
            }
            return results;
        }

        public IList<LeaveDto> GetRecordsByApprover(Guid approverId)
        {
            var records = from leaveRec in context.LeaveRecords where leaveRec.approverId == approverId select leaveRec;
            List<LeaveDto> results = new List<LeaveDto>();
            foreach (LeaveRecord record in records)
            {
                results.Add(ConvertTypes(record));
            }
            return results;
        }


        public IList<LeaveDto> GetRecordsByOwner(Guid ownerId)
        {
            var records = from leaveRec in context.LeaveRecords where leaveRec.ownerId == ownerId select leaveRec;
            List<LeaveDto> results = new List<LeaveDto>();
            foreach (LeaveRecord record in records)
            {
                results.Add(ConvertTypes(record));
            }
            return results;
        }

        public LeaveDto GetRecord(Guid instanceId) 
        {
            LeaveRecord record = (from leaveRec in context.LeaveRecords where leaveRec.workflowInstanceId == instanceId select leaveRec).First();
            return ConvertTypes(record);
        }

        public void InsertRecord(LeaveDto leave)
        {
            context.LeaveRecords.InsertOnSubmit(ConvertTypes(leave));
            context.SubmitChanges();            
        }

        private LeaveDto ConvertTypes(LeaveRecord leaveRec)
        {
            return new LeaveDto()
            {
                CurrentStateName = leaveRec.currentState,
                PendingRecommenderId = leaveRec.nextRecommenderId,
                OwnerId = leaveRec.ownerId,
                WorkflowInstanceId = leaveRec.workflowInstanceId,
                ApproverId = leaveRec.approverId
            };
        }

        private LeaveRecord ConvertTypes(LeaveDto leaveDto)
        {
            return new LeaveRecord() { 
                currentState = leaveDto.CurrentStateName,
                nextRecommenderId = leaveDto.PendingRecommenderId,
                ownerId = leaveDto.OwnerId,
                workflowInstanceId = leaveDto.WorkflowInstanceId,
                approverId = leaveDto.ApproverId
            };
        }



    }
}
